"""
需求：采集下厨房本周最受欢迎食谱信息列表
"""
import requests
from lxml import etree
import re

headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36',
    'Cookie': 'BDUSS_BFESS=0NKak04NHF0TXlKYjJIc1pmUG1Kb2prUDNVZmxFQll0T3JicGlOenlYc2lGVGRuRVFBQUFBJCQAAAAAAAAAAAEAAAC9nT0Bd2VsdW4AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACKID2ciiA9nV; BAIDUID_BFESS=489E1340B475CF28670858E4CB8776D4:FG=1; ZFY=sX8ZyMTLaxLY9YGtvKB5TQJU42GsGUOXF05rjNEm1R0:C; BCLID_BFESS=7169588048830265807; BDSFRCVID_BFESS=qSCOJexroGWPr_nJS7grtKbhYeKK0gOTDYrEmyKS-nqv_1tVYeHVEG0PtOi2xEPM4ch-ogKKBeOTHn0F_2uxOjjg8UtVJeC6EG0Ptf8g0M5; H_BDCLCKID_SF_BFESS=JR4H_CIhfCK3fP36q4Oo5tD_hgT22-us-G7m2hcHMPoosU3MKln_ylJWbp5JB-j3yCTiaKJjBMbUotoHhJrv0IuHKqjdJR3pyCT7-q5TtUJM8nI42MomXj0SM2cyKMni0DT9-pn8tpQrh459XP68bTkA5bjZKxtq3mkjbPbDfn028DKu-n5jHjjQjGDe3f; H_WISE_SIDS_BFESS=61027_61099_61301_60851_61367_61392'
}
for i in range(1, 21):
    url = f'https://www.xiachufang.com/explore/?page={i}'
    res = requests.get(url, headers=headers)
    html = res.content.decode()
    # print(html)
    # 使用xpath解析页面
    root = etree.HTML(html)
    result = root.xpath('//ul[@class="list"]/li/div/div')
    print(result)
    for div in result:
        food_name = "".join(div.xpath('.//p[@class="name"]/a/text()'))
        name = re.sub(r'\s', "", food_name)
        print("食谱名称：", name)
        # ycl = "".join(div.xpath('.//p[@class="ing ellipsis"]/a/text()'))
        # print("原材料：", ycl)
        link = "".join(div.xpath('.//p[@class="name"]/a/@href'))
        link = "https://www.xiachufang.com" + link
        print(link)
        author = "".join(div.xpath('.//p[@class="author"]/a/text()'))
        print("作者：", author)